<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="edu.ccut.saturn.component.SaturnData"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<%@ include file="/extjs/include/all.jsp"%>
<title></title>
<script type="text/javascript">
Ext.onReady(function(){
	var userId = '${__SATURN_AUTORITY_USER__.id}';
	var resultWidth = Ext.get("form-ct").getWidth();
	var simple = new Ext.FormPanel({
			renderTo:'form-ct',
			//autoHeight:true,
			height:100,
	        frame:true,
	        title: '查询条件',
	        labelWidth: 60,
	        bodyStyle:'width:100%',
	       items: [{
	            layout:'column',
	            items:[{
	                columnWidth:.25,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '委托人',
		                    name: 'delegate',
		                    id: 'delegate',
		                    readOnly:true
		                },{
		                    xtype:'hidden',
		                    name: 'agent_from',
		                    id: 'agent_from'
		                }]
	            },{
	                columnWidth:.2,
	                layout: 'form',
	                items: [{
		                    xtype:'button',
		                    text: '选择委托人',
		                    handler:chooseDelegate
		                }]
	            },{
	                columnWidth:.25,
	                layout: 'form',
	                items: [{
		                    xtype:'textfield',
		                    fieldLabel: '代理人',
		                    name: 'agent',
		                    id: 'agent',
		                    readOnly:true
		                },{
			                 name:'agent_to_type',
			                 id:'agent_to_type',
				        	 xtype:'hidden'
					      },{
		                    xtype:'hidden',
		                    name: 'agent_to',
		                    id: 'agent_to'
		                }]
	            },{
	                columnWidth:.2,
	                layout: 'form',
	                items: [{
		                    xtype:'button',
		                    text: '选择代理人',
		                    handler:chooseAgent
		                }]
	            }] 
        }],
        tbar:new Array(
			   new Ext.Toolbar.Button({
						   text:'查询',
						   xtype:"button" ,
						   tooltip:"按条件查询" ,
						   handler:queryByCondition,
						   scope :this}),
			   new Ext.Toolbar.Button({
						   text:'重置',
						   handler:resetForm,
						   xtype:"button" ,
						   tooltip:"恢复默认值",
						   scope :this}) 
			 ) 
	    });
    var sm = new Ext.grid.CheckboxSelectionModel();
	var cm = new Ext.grid.ColumnModel([
	    new Ext.grid.RowNumberer(),
	    sm, 
	   {header: "代理ID", width:resultWidth*0.1,dataIndex: 'agent_id', sortable: true},
       {header: "代理人",width:resultWidth*0.1,dataIndex: 'agent_to', sortable: true},
       {header: "代理方式",width:resultWidth*0.1,dataIndex: 'agent_type', sortable: true,renderer:showType},
       {header: "生效时间",width:resultWidth*0.15,dataIndex: 'start_time', sortable: true},
       {header: "终止时间",width:resultWidth*0.15,dataIndex: 'end_time', sortable: true}
	 ]); 
	cm.defaultSortable = true; 
	var store = new Ext.data.JsonStore({
		    url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/agentManagerModule/action/QueryAgentListByCondition.action',
		    root: 'result',
		    totalProperty:'count',
		    fields: [
		   		   {name: 'agent_id', mapping: 'agent_id'},
	               {name: 'agent_to', mapping: 'agent_to'},
	               {name: 'agent_type', mapping: 'agent_type'},
	               {name: 'start_time', mapping: 'start_time'}, 
	               {name: 'end_time', mapping: 'end_time'}
	               ]
		});
		store.load({params: {start:0,limit:20,agent_from:Ext.get("agent_from").dom.value,
					agent_to:Ext.get("agent_to").dom.value,agent_to_type:Ext.get("agent_to_type").dom.value}});

		store.on('beforeload',function(){
		    	Ext.apply(this.baseParams,{agent_from:Ext.get("agent_from").dom.value,agent_to:Ext.get("agent_to").dom.value,agent_to_type:Ext.get("agent_to_type").dom.value});
	    });
	    
		var grid = new Ext.grid.GridPanel({
			    bodyStyle:'width:100%',
	       	    store: store,
			    sm:sm,
			    cm:cm,
		        renderTo:document.body,
		        height:500,
		        frame:true,
		        loadMask: {msg:'正在加载数据，请稍侯……'},
        		bbar: new Ext.PagingToolbar({ 
		            pageSize: 20, 
		            store: store, 
		            displayInfo: true, 
		            displayMsg: '当前显示 {0} - {1}条记录 /共 {2}条记录',
		            emptyMsg: "没有数据显示" 
	    		}),
	    		tbar:new Array(
					new Ext.Toolbar.Button({
						   text:'删除代理',
						   xtype:"button" ,
						   tooltip:"删除代理关系" ,
						   handler: deleteAgent,
						   scope :this}),
			   		new Ext.Toolbar.Button({
						   text:'添加完全代理',
						   xtype:"button" ,
						   tooltip:"添加完全代理" ,
						   handler: addAll,
						   scope :this}),
			  		new Ext.Toolbar.Button({
						   text:'添加部分代理',
						   xtype:"button" ,
						   tooltip:"添加部分代理" ,
						    handler: addPart,
						   scope :this})
		 ) 
		});
		
    	//完全代理	
       function addAll(){	
       		window.location="allAgent.jsp";
       }
       
       //部分代理	
       function addPart(){	
       		window.location="partAgent.jsp";
       }
       
       function deleteAgent(){
		    var agentIds = sm.getSelections();
		 	if(agentIds.length==0){
		 		Ext.MessageBox.alert('警告','至少选择一条记录');
		 	}else{
		 		Ext.MessageBox.confirm('确认','确定删除所选择的数据?',delAgent);
		 	}
	   }
	  //删除代理
	 function delAgent(btn){
	 	if(btn == 'yes'){
	        arr_agentid = sm.getSelections();
	        var delStr = ''; 		 
	   	   	for (var i = 0; i < arr_agentid.length; i++){    	   	   	  	 
	   	   	  	 delStr += arr_agentid[i].data["agent_id"];  
	   	   	  	 delStr += ',' ;	
			} 
		    if(delStr.length > 0){
 					delStr = delStr.substring(0,delStr.length - 1) ;
			}
			Ext.Ajax.request({
			     method : 'POST',                           
			     url: '${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/agentManagerModule/action/DeleteAgent.action',     
			     success:function(request){               
			       var myResult = Ext.util.JSON.decode(request.responseText);
			       Ext.Msg.alert('信息',myResult.message,function(){
			         //window.location.reload();
			         store.load();
			         });        
			     },
			     failure:function(){                          
			       Ext.Msg.alert("错误", "与后台联系的时候出现了问题");
			     },
			     params:{
			     		agentIds:delStr 
			     } 
	     	});
       	}
	 }
	 
	 function chooseDelegate(){
			var root=new Ext.tree.AsyncTreeNode({
				id:"root_participant",
				text:"参与者"});
			var tree=new Ext.tree.TreePanel({
				root:root,
				loader: new Ext.tree.TreeLoader({url:'${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/agentManagerModule/action/GetTreeJson.action'}),
				width:400,
				autoScroll: true
			});	

		  tree.on('click', function(node){
					addDelegate(node.id,node.text);
				},this);
	       var window = new Ext.Window({
				title: '添加代理人',
				width: 500,
				height:300,
				minWidth: 300,
				minHeight: 200,
				layout: 'fit',
				plain:true,
				items:tree,
				buttonAlign:'center',
				modal:true
			});
			window.show();
		}
		
		
	 function addDelegate(userid,name){
	 	 if(userid=='root_participant'){
			return false;
		 }
		 if(userid!='rootDept'&& userid!='rootRole'){
		 	var type = userid.split('_')[0];
		 	var id = userid.split('_')[1];
		 	if(type=='deptUser'||type=='roleUser'){
		 		Ext.get("agent_from").dom.value =id;
		 		Ext.get("delegate").dom.value =name;
		 	}
		 }
	 }
	 
	  function chooseAgent(){
			var root=new Ext.tree.AsyncTreeNode({
				id:"root_participant",
				text:"参与者"});
			var tree=new Ext.tree.TreePanel({
				root:root,
				loader: new Ext.tree.TreeLoader({url:'${pageContext.request.contextPath}/edu.ccut.saturn.workflow.manager::/agentManagerModule/action/GetTreeJson.action'}),
				width:400,
				autoScroll: true
			});	

		  tree.on('click', function(node){
					addAgent(node.id,node.text);
				},this);
	       var window = new Ext.Window({
				title: '添加代理人',
				width: 500,
				height:300,
				minWidth: 300,
				minHeight: 200,
				layout: 'fit',
				plain:true,
				items:tree,
				buttonAlign:'center',
				modal:true
			});
			window.show();
		}
	 
	 	function addAgent(userid,name){
		 	if(userid=='root_participant'){
				return false;
			 }
			if(userid!='rootDept'&& userid!='rootRole'){
			 	var type = userid.split('_')[0];
			 	var id = userid.split('_')[1];
			 	if(type=='deptUser'||type=='roleUser'){
			 		Ext.get("agent_to_type").dom.value ="user";
			 		Ext.get("agent_to").dom.value =id;
			 		Ext.get("agent").dom.value =name;
			 	}
			 	else if(type=='dept'){
			 		Ext.get("agent_to_type").dom.value ="dept";	
			 		Ext.get("agent_to").dom.value =id;
			 		Ext.get("agent").dom.value =name; 		
			 	}
			 	else if(type=='role'){
			 		Ext.get("agent_to_type").dom.value ="role";
			 		Ext.get("agent_to").dom.value =id;
			 		Ext.get("agent").dom.value =name;
			 	}
			 	
			 }
	 }
	 
	 function queryByCondition(){
   		var agent_to = Ext.get("agent_to").dom.value;
   		var agent_to_type = Ext.get("agent_to_type").dom.value;
   		var agent_from = Ext.get("agent_from").dom.value;
   		store.load({params: {start:0,limit:20,agent_from:agent_from,
						agent_to:agent_to,agent_to_type:agent_to_type}});
	 }
	 
	 function resetForm(){
	 	simple.getForm().reset();
	 }
	 
	  
	 function showType(value){
	 	if(value=='PART'){
	 		return '部分代理';
	 	}
	 	if(value=='ALL'){
	 		return '完全代理';
	 	}
	 }
});
</script>
<div id='form-ct' width='100%'></div>
<div id='task-grid' width='100%'></div>
</head>
</html>